package goods;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import org.junit.Before;
import org.junit.Test;

public class GoodsTest {
	List<Goods> goods = new ArrayList<>();
	
	@Before
	public void init() {
		// 服装，电器，食品
		goods.add(new Goods("冰柜","电器",100,1002));
		goods.add(new Goods("彩电","电器",100,1003));
		goods.add(new Goods("衬衫","服装",100,1004));
		goods.add(new Goods("果冻","食品",20,1004));
		goods.add(new Goods("薯片","食品",10,100));
		goods.add(new Goods("酱油","食品",20,100));
		goods.add(new Goods("耗油","食品",30,100));
		goods.add(new Goods("万能充","电器",50,100));
		goods.add(new Goods("裤衩子","服装",666,100));
		goods.add(new Goods("热得快","电器",23,0));
		goods.add(new Goods("绿帽子","服装",0,0));
		goods.add(new Goods("苹果13","电器",7000,999));
		goods.add(new Goods("SMG","服装",200,375));
		goods.add(new Goods("红烧牛肉面","食品",2,999));
		goods.add(new Goods("华为","电器",3999,862));
		goods.add(new Goods("AJ","服装",500,5000));
		goods.add(new Goods("乐事薯片","食品",3,9999));
		goods.add(new Goods("苹果12","电器",8614,9999));
		goods.add(new Goods("亲嘴烧","食品",1,3845));
		goods.add(new Goods("阿迪达斯","服装",120,397));
		goods.add(new Goods("奥利奥夹心饼干","食品",10,348));
		goods.add(new Goods("羽绒服", "服装", 899, 22));
		goods.add(new Goods("羊毛大衣", "服装", 3560, 14));
		goods.add(new Goods("电饭煲", "电器", 569, 38));
		goods.add(new Goods("泡椒凤爪", "食品", 7, 55));
		goods.add(new Goods("原味乐事薯片", "食品", 10, 200));

	}
	
	@Test
	public void test() {
//		1.查询商品数量
		long count = goods.stream().count();
		System.out.println(count);
		System.out.println("-----------------------------");
//		2.查询单价小于10元的商品数量
		long count2 = goods.stream().filter(g->g.getPrice()<10).count();
		System.out.println(count2);
		System.out.println("-----------------------------");
//		3.有几种商品类型
		int size = goods.stream().collect(Collectors.groupingBy(g -> ((Goods)g).getType())).size();
		System.out.println(size);
		System.out.println("-----------------------------");
//		4.把商品数量为0的过滤掉
		goods.stream().filter(g -> g.getCount() > 0).forEach(g->System.out.println(g));
		System.out.println("-----------------------------");
//		5.求所有商品的数量总和
		Integer collect = goods.stream().collect(Collectors.summingInt(g-> ((Goods)g).getCount()));
		System.out.println(collect);
		System.out.println("-----------------------------");
//		6.求所有商品的价格总和（不是单价总和）
		Integer collect2 = goods.stream().
			collect(Collectors.summingInt(g-> ((Goods)g).getCount() * ((Goods)g).getPrice()));
		System.out.println(collect2);
		System.out.println("-----------------------------");
//		7.求服装类别的商品超过100元的商品
		/*
		 *  g.getType().equals("服装");   // 抛NPE空指针异常 
		 *  Objects.equals("服装", g.getType())  // 不会产生NPE
		 *  "服装".equals(g.getType())   // 不会产生NPE
		 */
		goods.stream().filter(g -> Objects.equals("服装", g.getType()) && g.getPrice() > 100)
		    .forEach(g->System.out.println(g));
		System.out.println("-----------------------------");
//		8.根据商品类别分组
		Map<String, List<Goods>> collect3 = goods.stream().collect(Collectors.groupingBy(g -> ((Goods)g).getType()));
		collect3.forEach((k,v)->System.out.println(k +"\n\t:" + v));
		System.out.println("-----------------------------");
//		9.根据商品类别分组后求每种商品数量
		Map<String, Long> collect4 = goods.stream().collect(Collectors.groupingBy(g -> ((Goods)g).getType(),
				                                    Collectors.counting()));
		collect4.forEach((k,v)->System.out.println(k +"\n\t:" + v));
		System.out.println("-----------------------------");
//		10.根据商品类别分组后求每组商品的数量总和
		Map<String, Integer> collect5 = goods.stream().collect(Collectors.groupingBy(g -> ((Goods)g).getType(),
							Collectors.summingInt(g->((Goods)g).getCount())));
		collect5.forEach((k,v)->System.out.println(k +"\n\t:" + v));
		System.out.println("-----------------------------");
//		11.根据商品类别分组后求每组商品最贵的价格
		Map<String, Optional<Goods>> collect6 = goods.stream().collect(Collectors.groupingBy(g -> ((Goods)g).getType(),
							Collectors.maxBy((g1,g2)->((Goods)g1).getPrice()-((Goods)g2).getPrice())));
		collect6.forEach((k,v)->System.out.println(k +"\n\t:" + v));
		System.out.println("-----------------------------");
//		12.查询商品名里有"电"的商品
		goods.stream().filter(g -> g.getName().contains("电")).forEach(g->System.out.println(g));
		System.out.println("-----------------------------");
//		13.查询商品名里有"电"的商品的数量
		long count3 = goods.stream().filter(g -> g.getName().contains("电")).count();
		System.out.println(count3);
		System.out.println("-----------------------------");
//		14.根据商品单价从大到小排序
		goods.stream().sorted((g1,g2)-> g2.getPrice()-g1.getPrice()).forEach(g->System.out.println(g));
		System.out.println("-----------------------------");
//		15.根据商品数量从小到大排序后求前5名
		goods.stream().sorted((g1,g2)-> g1.getCount()-g2.getCount()).limit(5).forEach(g->System.out.println(g));
		System.out.println("-----------------------------");
		
//		16.根据商品类别分组求每类商品的平均单价
		Map<String, Double> collect7 = goods.stream().collect(Collectors.groupingBy(g-> ((Goods)g).getType(),
				Collectors.averagingDouble(g -> ((Goods)g).getPrice())));
		collect7.forEach((k,v)->System.out.println(k +"\n\t:" + v));
		System.out.println("-----------------------------");
//		17.根据商品类别分组后，每组里商品再根据单价从小到大排序
		Map<String, List<Goods>> collect8 = goods.stream().sorted((g1,g2)-> g1.getPrice()-g2.getPrice())
			.collect(Collectors.groupingBy(g-> ((Goods)g).getType()));
		collect8.forEach((k,v)->System.out.println(k +"\n\t:" + v));
		System.out.println("-----------------------------");
//		18.把所有商品名提取出来拼接成字符串
		String collect9 = goods.stream().map(g -> g.getName()).collect(Collectors.joining(",","[","]"));
		System.out.println(collect9);
		System.out.println("-----------------------------");
//		19.把每个商品数量+200
		goods.stream().map(g -> g.getCount()+200).forEach(t->System.out.println(t));
		System.out.println("-----------------------------");
//		20.统计商品价格在100~200之间的商品的数量
		long count4 = goods.stream().filter(g -> g.getPrice()>=100 && g.getPrice()<=200).count();
		System.out.println(count4);
	}
	
}
